package demo3;

import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2024-06-11
 * Time: 17:10
 */
class Teacher{
    public String name;
    public int age;

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    /*@Override//用来指定比较大小的方式
    public int compareTo(Student o) {
        //return this.age-o.age;//根据年龄大小比较
        if(this.age > o.age){
            return 1;
        } else if (this.age < o.age) {
            return -1;
        }else {
            return 0;
        }*/
}

//根据年龄进行比较
class AgeComparator implements Comparator<Teacher>{
    @Override
    public int compare(Teacher o1, Teacher o2) {
        return o1.age - o2.age;
    }
}
//根据姓名进行比较
class NameComparator implements Comparator<Teacher>{
    @Override
    public int compare(Teacher o1, Teacher o2) {
        return o1.name.compareTo(o2.name);
    }
}

public class Test01 {
    public static void main(String[] args) {
        Teacher teacher1 = new Teacher("小明",15);
        Teacher teacher2 = new Teacher("小明",20);

        //根据年龄进行比较
        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(teacher1, teacher2));

        //根据姓名进行比较，输出正值表示大于，负表示小于，0表示相同
        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(teacher1, teacher2));

    }
}
